import { getClassAndStyleList, getUserPersona } from '../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },
  data: {
    showStyle: true,
    showCate: false,
    styleList: [],
    cateList: []
  },
  methods: {
    async getList () {
      try {
        const { ResultSet } = await getClassAndStyleList()
        ResultSet.userData.stylesList.forEach(item => {
          item.isActive = false
        })
        ResultSet.userData.firstClassificationList.forEach(item => {
          item.isActive = false
        })
        this.setData({
          styleList: ResultSet.userData.stylesList,
          cateList: ResultSet.userData.firstClassificationList
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    selectStyle ({ currentTarget: { dataset: { item, index } } }) {
      item.isActive = !item.isActive
      this.data.styleList[index] = item
      this.setData({ styleList: this.data.styleList })
    },
    selectCate ({ currentTarget: { dataset: { item, index } } }) {
      item.isActive = !item.isActive
      this.data.cateList[index] = item
      this.setData({ cateList: this.data.cateList })
    },
    next () {
      this.setData({ showStyle: false, showCate: true })
    },
    prev () {
      this.setData({ showStyle: true, showCate: false })
    },
    skip () {
      this.triggerEvent('close')
    },
    async confirm () {
      try {
        let styleArray = []
        let classificationArray = []
        this.data.styleList.forEach(item => {
          if (item.isActive) styleArray.push(item.styleId)
        })
        this.data.cateList.forEach(item => {
          if (item.isActive) classificationArray.push(item.classificationId)
        })
        await getUserPersona({ styleArray, classificationArray })
        await wx.$pro.setStorage({ key: 'FIRSTLOGIN', data: false })
        this.skip()
      } catch (err) {
        console.log(err)
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
  },
  created () {
    this.getList()
  },
})